const app = getApp();
Page({
  data: {
    xhx_images: app.xhx_images,
    xhx_route: app.xhx_route,
  },
  onLoad,
  updateLetterStatus,//更新私信阅读状态未已读
  onPullDownRefresh, //页面下拉触底事件的处理函数
  onReachBottom,//上拉刷新
  listDataInit,
  getPageList, //获取列表
  ifPrev5Minute,//判断时间是否在5分钟内
  send, //发送信息
  onShareAppMessage: function() {}
})

function onLoad(options) {
  const that = this;
  const username = options && options.username || "";
  const receiver = options && options.receiver || "";
  const headPortrait = options && options.headPortrait || "";
  wx.setNavigationBarTitle({
    title: username,
  })
  that.setData({
    ohterName: username,
    receiver: receiver,
    otherHeadPortrait: headPortrait,
    userId: app.userInfo.userId,
    headPortrait: app.userInfo.headPortrait,
  })
  that.listDataInit();
  that.updateLetterStatus();
}

function updateLetterStatus(){
  const that = this;
  const receiver = that.data.receiver;
  var reqData ={
    otherAccount: receiver
  }
  app.middleware.requestAjax({
    serAddr: app.serAddrs.updateLetterStatus,
    data:reqData,
    cancelToast: true,
    success: function (res) {
      app.getNotifyCount();
    }
  })
}

function onPullDownRefresh() {
  const that = this;
  that.getPageList(true);
}

function onReachBottom(){
  const that = this;
  that.listDataInit();
}

function listDataInit() {
  const that = this;
  that.setData({
    keyword: '', //搜索框输入值
    // lists: [], //列表数据
    msg: '请稍等...', //提示信息
    footMsg: "没有更多数据啦！", //底部提示信息
    page: 1, //当前页数
    row: 10, //1页的条数
    isReach: false, //是否全部加载完毕
  })
  that.getPageList();
  return true;
}

/**
 * 获取列表
 * param isReachBottom [Boolean] 是否上拉加载触发
 */
function getPageList(isReachBottom) {
  var that = this;
  var receiver = that.data.receiver;
  var lists = that.data.lists;
  var row = that.data.row;
  var page = that.data.page;
  var keyword = that.data.keyword;
  var isReach = that.data.isReach;
  var footMsg = "";

  if (isReachBottom) {
    if (isReach) {
      wx.stopPullDownRefresh();
      return false;
    }
    page++;
  }

  if (page == 1) {
    lists = [];
  }

  var reqData = {
    pageSize: row,
    pageNum: page,
    receiver: receiver,
  }

  app.middleware.requestAjax({
    serAddr: app.serAddrs.latterList,
    data: reqData,
    cancelToast: true,
    success: function(res) {
      console.log(res)
      var tempLists = res.data || [];

      for(var i=0;i<tempLists.length;i++){
        if (that.ifPrev5Minute(tempLists[i].sendTime*1000)){
          tempLists[i].showTime = true;
        }
        tempLists[i].sendTime = app.middleware.time(0, tempLists[i].sendTime * 1000).typeD;
      }
      
      tempLists.reverse();

      if (row <= tempLists.length) {
        isReach = false;
        footMsg = "上拉加载更多";
      } else {
        isReach = true;
        footMsg = "没有更多数据啦！";
      }
      that.setData({
        msg: "",
        page: page,
        lists: tempLists.concat(lists),
        isReach: isReach,
        footMsg: footMsg,
      })

      if (page == 1) {
        wx.pageScrollTo({
          scrollTop: 1000,
        })
      }else{
        wx.stopPullDownRefresh()
      }
    },
    fail: function(res) {

      if (isReachBottom) {
        that.setData({
          footMsg: res && res.msg || "加载失败",
        })
      } else {
        that.setData({
          msg: res && res.msg || "加载失败",
        })
      }

    }
  })
}

function send(event) {
  const that = this;
  const content = event.detail.value.content.trim();
  const receiver = that.data.receiver;
  const lists = that.data.lists || [];
  const userId = that.data.userId || 0;

  if(!content) return;

  lists.push({
    content: content,
    receiver: receiver,
    sendTime: app.middleware.time().typeD,
    sender: userId,
    uuid: app.middleware.time().msec,
    showTime: that.ifPrev5Minute(app.middleware.time().msec)
  })

  that.setData({
    lists,
    content:"",
  })

  wx.pageScrollTo({
    scrollTop: lists.length*100,
  })

  let reqData={
    receiver,
    content,
  }

  app.middleware.requestAjax({
    serAddr: app.serAddrs.latterAdd,
    data:reqData,
    cancelToast: true,
    fail: function(res) {
      lists[lists.length-1].error=true;
      that.setData({
        lists,
      })
    }
  })
}

function ifPrev5Minute(t){
  const that = this;
  var fiveMinuteMsec = 1000 * 60 * 5; //5分钟的毫秒
  var prevShowTime = that.data.prevShowTime;

  if (!prevShowTime || (app.middleware.time(0, prevShowTime).msec - app.middleware.time(0, t).msec > fiveMinuteMsec)) {
    prevShowTime = t;
    that.setData({
      prevShowTime
    })
    return true;
  }else{
    return false;
  }
}